package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author atguigu-mqx
 */
@RestController
@RequestMapping("api/cart")
public class CartApiController {

    @Autowired
    private CartService cartService;

    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request){
        // 获取用户id
        String userId = AuthContextHolder.getUserId(request);
        // 判断，如果userId为空的话，则表示没有登录，则生成零食的用户id
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }

        // 调用service层
        cartService.addToCart(skuId,userId,skuNum);
        // 返回数据
        return Result.ok();
    }

    /**
     * 查询购物车数据
     * @param request
     * @return
     */
    @GetMapping("cartList")
    public Result cartList (HttpServletRequest request){

        // 获取userid和零时id
        String userId = AuthContextHolder.getUserId(request);
        //  获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        // 调用服务层
        List<CartInfo> cartList = cartService.getCartList(userId, userTempId);
        // 返回结果
        return Result.ok(cartList);
    }

    /**
     * 设置选中状态
     * @param skuId
     * @param isChecked
     * @param request
     * @return
     */
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable Long skuId,
                            @PathVariable Integer isChecked,
                            HttpServletRequest request){
        // 获取用户id
        String userId = AuthContextHolder.getUserId(request);

        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }
        // 调用service
        cartService.checkCart(skuId,userId,isChecked);
        // 返回结果
        return Result.ok();
    }

    //  删除购物车：
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        // 获取用户id
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isEmpty(userId)){
            userId = AuthContextHolder.getUserTempId(request);
        }

        // 调用service
        cartService.deleteCart(skuId,userId);

        // 返回结果
        return Result.ok();
    }


    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable(value = "userId") String userId) {
        return cartService.getCartCheckedList(userId);
    }
}
